#!/usr/bin/env python3
# -*- encoding: utf-8 -*-
# 文件: getdstversion.py
# 说明:
# 时间: 2021/11/07 17:35:04
# 作者: Azure
# 版本: 1.0

'''
获取最新版的饥荒服务器版本，并写入文件'''

from tools.dstversion import newest
from os import makedirs
from os.path import exists

if not exists('./temp'):
    makedirs('./temp')

filepath = './temp/dstversion.txt'
print('正在查询，请稍后...')

try:
    with open('./version.txt') as fr:
        a = fr.read().strip()
except:
    a = ''

try:
    news = newest(1)
except Exception as e:
    print(f"查询版本时错误: {e}")
    print(f"当前版是: {a}")
    raise e

with open(filepath, 'w') as fr:
    fr.write(str(news) + '\n')


print(f'最新版本：{news}')
print(f'当前版本：{a}')

try:
    if int(a) > int(news):
        print('应该无需操作')
    elif int(a) < int(news):
        print('需要更新')
    else:
        print('最新版本')
except:
    if str(news) != a:
        print('需要更新！')
    else:
        print('无需操作！')
